<?php
namespace Api\Controller;

use Think\Controller;

/**
 * 订单
 */
class OrderController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 订单状态字典
     * @return [type] [description]
     */
    public function status()
    {
        $status = D('Api/Order', 'Service')->status();
        success($status);
    }

    /**
     * 预查看订单
     * @return [type] [description]
     */
    /*public function preView()
    {
    $goods_id     = I('post.goods_id', 0, 'intval');
    $spec_item_id = I('post.spec_item_id', 0, 'intval');
    $number       = I('post.number', 0, 'intval');

    $buy_now = $goods_id && $number ? true : false;

    $cart_ids = I('post.cart_ids', '', 'trim');
    if (!($cart_ids || $buy_now)) {
    error(8045, '无效操作，无法查看订单');
    }
    }*/

    /**
     * 查找订单
     * @param string $value [description]
     */
    public function index()
    {
        $type = I('post.type', 0, 'intval');
        $page = I('post.page', 1, 'intval');

        $orders = D('Api/Order', 'Service')->search(USER_ID, $type, $page);
        success($orders);
    }

    /**
     * 生成订单
     * @return [type] [description]
     */
    public function create()
    {
        $goods_id     = I('post.goods_id', 0, 'intval');
        $spec_item_id = I('post.spec_item_id', 0, 'intval');
        $number       = I('post.number', 0, 'intval');
        $coupon_id    = I('post.coupon_id', 0, 'intval');
        $address_id   = I('post.address_id', 3, 'intval');

        // 立即购买还是购物车购买
        $buy_now = $goods_id && $number ? true : false;

        $cart_ids = I('post.cart_ids', '22,23', 'trim');
        if (!($cart_ids || $buy_now)) {
            error(8045, '无效操作，无法创建订单');
        }

        if (!$address_id) {
            error(8048, '收货地址有误');
        }

        // 把立即购买的商品数据当作购物车数据处理
        $cart = [];
        if ($buy_now) {
            $is_spec_item = M('goods')
                ->where(['id' => $goods_id])
                ->getField('is_spec');

            $g_id = M('goods_spec_item')
                ->where(['id' => $spec_item_id])
                ->getField('goods_id');

            if ($is_spec_item && $g_id != $goods_id) {
                error(8046, '数据有误');
            }

            $cart[] = [
                'shop_id'      => M('goods')->where(['id' => $goods_id])->getField('shop_id'),
                'goods_id'     => $goods_id,
                'spec_item_id' => $spec_item_id,
                'number'       => $number,
            ];
        } else {
            if ($cart_ids) {
                $cart = M('cart')
                    ->field('shop_id,goods_id,spec_item_id,number')
                    ->where([
                        'id'        => ['in', explode(',', $cart_ids)],
                        'is_delete' => 0,
                    ])->select();
            }
        }

        // 商品不存在了
        if (!count($cart)) {
            error(333, '');
        }

        $order_id = 0;
        do {
            begin();
            try {

                $order_id = D('Api/Order', 'Service')->create(USER_ID, $cart, $address_id, $coupon_id);

            } catch (Exception $e) {
                rollback();
            }
            commit();
        } while (!$order_id);

        if (!$order_id) {
            error(8047, '创建订单失败');
        }

        $order_no = M('order')->where(['id' => $order_id])->getField('order_no');

        success($order_no);
    }

    /**
     * 订单详情
     * @return [type] [description]
     */
    public function detail()
    {
        $order_no = I('post.order_no', '', 'trim');

        $order = D('Api/Order', 'Service')->detail(USER_ID, $order_no);
        success($order);
    }

    /**
     * 订单物流信息
     * @return [type] [description]
     */
    public function express()
    {
        $order_no = I('post.order_no', '', 'trim');

        $order = D('Api/Order', 'Service')->express($order_no);
        success($order);
    }

    /**
     * 评论商品
     * @return [type] [description]
     */
    public function goodsReview()
    {
        $order_no     = I('post.order_no', '', 'trim');
        $goods_id     = I('post.goods_id', 0, 'intval');
        $spec_item_id = I('post.spec_item_id', 0, 'intval');

        $goods_match     = I('post.goods_match', 0, 'intval');
        $shop_service    = I('post.shop_service', 0, 'intval');
        $express_service = I('post.express_service', 0, 'intval');

        $content = I('post.content', '', 'trim');

        if (!$order_no || !$goods_id) {
            error(8085, '订单数据有误');
        }

        if (!$goods_match || !$shop_service || !$express_service) {
            error(8086, '请选择一个评论星级');
        }

        if (!$content) {
            error(8087, '评论内容不能为空');
        }

        $result = upload('Review_Image');

        // 评论图片
        $images = [];
        if (isset($result['images'])) {
            foreach ($result['images'] as $img) {
                $images[] = C('DOMAIN') . $img['url'];
            }
        }

        $review = [
            'goods_match'     => $goods_match,
            'shop_service'    => $shop_service,
            'express_service' => $express_service,
            'content'         => $content,
        ];

        $res = D('Api/Order', 'Service')->review(USER_ID, $order_no, $goods_id, $spec_item_id, $review, $images);
        if ($res) {
            success();
        } else {
            error(8088, '评论出错了');
        }
    }

    /**
     * 投诉商家
     * @return [type] [description]
     */
    public function complaint()
    {
        $order_no = I('post.order_no', '1111233308500001', 'trim');

        $user = [
            'username' => $username = I('post.username', '陈先生', 'trim'),
            'mobile'   => $mobile = I('post.mobile', '15800158000', 'trim'),
        ];

        $complaint = [
            'reason'  => $reason = I('post.reason', '商家未按时发货', 'trim'),
            'content' => $content = I('post.content', '都说了今天11点前发货，都多少天了，还没发货', 'trim'),
        ];

        if (!$username || !is_phone($mobile)) {
            error(8091, '请填写投诉人信息');
        }

        if (!$reason || !$content) {
            error(8092, '请输入投诉信息');
        }

        // 投诉图片
        $images = [];
        if (isset($result['images'])) {
            foreach ($result['images'] as $img) {
                $images[] = C('DOMAIN') . $img['url'];
            }
        }

        $res = D('Api/Complaint', 'Service')->order(USER_ID, $order_no, $user, $complaint, $images);
        if ($res) {
            success();
        } else {
            error(8088, '投诉失败');
        }
    }
}
